using System;
using Kollektiv.Config;
using Kollektiv.DataModel;
using Kollektiv.DB;
using Kollektiv.Web.Mvc;
using Kollektiv.Validation;

namespace Kollektiv.Controller
{
	public class Configuration : Base
	{
        // /kollektiv/Configuration/InitDB?masterPassword=kollektiv&smtpFromAddress=user@gmail.com&smtpHost=smtp.gmail.com&smtpPort=465&smtpUsername=user@gmail.com&smtpPassword=password&smtpSSL=True
        [ControllerAction]
        public bool InitDB(
            string masterPassword,
            [EmailRPV]string smtpFromAddress,
            [NotNullOrEmptyPVA]string smtpHost,
            int smtpPort,
            string smtpUsername,
            string smtpPassword,
            bool smtpSSL)
        {
            try
            {
                DataService.Create(BaseDataServiceID);
                Initialize();
            }
            catch (Exception ex)
            {
                throw (ex);
            }

            BaseDataService.BeginTransaction();
            try
            {
                BaseDataService.GetRepository<SettingRepository>()
                    .Create("Kollektiv.Service.EmailService.fromAddress", smtpFromAddress);
                BaseDataService.GetRepository<SettingRepository>()
                    .Create("Kollektiv.Service.EmailService.smtpHost", smtpHost);
                BaseDataService.GetRepository<SettingRepository>()
                    .Create("Kollektiv.Service.EmailService.smtpPort", smtpPort.ToString());
                if (!string.IsNullOrEmpty(smtpUsername))
                {
                    BaseDataService.GetRepository<SettingRepository>()
                        .Create("Kollektiv.Service.EmailService.smtpUsername", smtpUsername);
                    BaseDataService.GetRepository<SettingRepository>()
                        .Create("Kollektiv.Service.EmailService.smtpPassword", smtpPassword);
                }
                BaseDataService.GetRepository<SettingRepository>()
                    .Create("Kollektiv.Service.EmailService.smtpSSL", smtpSSL.ToString());

                BaseDataService.GetRepository<SettingRepository>()
                    .Create("Default.Kollektiv.DataModel.User.Language", "en");

                BaseDataService.GetRepository<SettingRepository>()
                    .Create("Kollektiv.BaseController.DefaultLanguage", "en");

                Language englishLanguage = BaseDataService.GetRepository<LanguageRepository>().Create("en");

                BaseDataService.GetRepository<EmailContentRepository>().Create("Kollektiv.UserController.SendPassword", englishLanguage, "Your new password", "Here's your ne password: {0}", null);

                BaseDataService.GetRepository<UserRepository>()
                       .Create(Kollektiv.Controller.Session._GUEST, Kollektiv.Controller.Session._GUEST, "", "", englishLanguage);

                BaseDataService.CommitTransaction();
                return true;
            }
            catch (Exception ex)
            {
                BaseDataService.RollbackTransaction();
                throw (ex);
            }
        }
	}
}

